package lunceneChina;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystems;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import lunceneDemo.ReaderUtil;

public class CreateIndex {
	public static void main(String[] args) throws IOException, URISyntaxException {
		File file=new File("D:\\TestLunceneChina\\file");
		
		Directory directory=FSDirectory.open(FileSystems.getDefault().getPath("D:\\TestLunceneChina\\index_5.5"));
		Analyzer analyzer=new IKAnalyzer(true);
		
		IndexWriterConfig config=new IndexWriterConfig(analyzer);
		IndexWriter writer=new IndexWriter(directory, config);
		writer.deleteAll();
		
		
		
		File[] listFiles = file.listFiles();
		Document d=null;
		for (File file2 : listFiles) {
			d = new Document();
			d.add(new Field("content", ReaderUtil.txt2String(file2), TextField.TYPE_STORED));
			d.add(new TextField("filename", file2.getName(), Field.Store.YES));
			d.add(new TextField("filepath", file2.getPath(), Field.Store.YES));
			writer.addDocument(d);
		}
		writer.flush();
		writer.commit();
		writer.close();
		
		
		
	}
}
